//
//  LNProgressHUD.m
//  LenovoCochat
//
//  Created by wangxinwei on 2018/11/13.
//  Copyright © 2018 www.lenovo.com. All rights reserved.
//

#import "LNProgressHUD.h"
#import "LoadingImageView.h"
#import "SVProgressHUD+ShowImage.h"
static const CGFloat bgViewWidth = 125.0f;  //背景视图的宽度/高度
static const CGFloat kTextFontSize = 16.0f; // 文字大小

@implementation LNProgressHUD

+ (instancetype)sharedHUD {
    static LNProgressHUD *hud;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        hud = [[LNProgressHUD alloc] initWithView:[UIApplication sharedApplication].keyWindow];
    });
    
    return hud;
}

+ (void)showStatus:(LNProgressHUDStatus)status text:(NSString *)text isAutoHiden:(BOOL)isAutoHiden {
    
    if ([SVProgressHUD isVisible]) {
        [SVProgressHUD dismissLong];
    }
    dispatch_async(dispatch_get_main_queue(), ^{
        LNProgressHUD *progressHUD = [LNProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
        progressHUD.label.text = @"";
        NSDictionary *fontAttributeDic = @{NSLigatureAttributeName:@(0)};
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:text attributes:fontAttributeDic];
        [attributedString addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:kTextFontSize]} range:NSMakeRange(0, attributedString.length)];
        progressHUD.label.attributedText = attributedString;
        progressHUD.label.font = [UIFont systemFontOfSize:kTextFontSize];
        progressHUD.label.textColor = [UIColor blackColor];
        progressHUD.label.numberOfLines = 0;
        [progressHUD setMinSize:CGSizeMake(bgViewWidth, bgViewWidth)];
        progressHUD.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
        progressHUD.backgroundView.color = [UIColor colorWithWhite:0.f alpha:0.3f];
        progressHUD.bezelView.color = [UIColor whiteColor];
        progressHUD.margin = 10;
        switch (status) {
            case LNProgressHUDStatusSuccess: {
                progressHUD.mode = MBProgressHUDModeCustomView;
                LoadingImageView *successIcon = [[LoadingImageView alloc]initWithImage:[UIImage imageNamed:@"hub_success_check"]];
                progressHUD.customView = successIcon;
                if (isAutoHiden) {
                    [progressHUD hideAnimated:YES afterDelay:1.5f];
                }
            }
                break;
            case LNProgressHUDStatusError: {
                progressHUD.mode = MBProgressHUDModeCustomView;
                progressHUD.label.font = [UIFont systemFontOfSize:15];
                progressHUD.label.textColor = [UIColor blackColor];
                LoadingImageView *successIcon = [[LoadingImageView alloc]initWithImage:[UIImage imageNamed:@"hub_error"]];
                progressHUD.customView = successIcon;
                if (isAutoHiden) {
                    [progressHUD hideAnimated:YES afterDelay:1.5f];
                }
            }
                break;
            case LNProgressHUDStatusInfo:
                progressHUD.mode = MBProgressHUDModeCustomView;
                progressHUD.label.font = [UIFont systemFontOfSize:14];
                progressHUD.label.textColor = [UIColor whiteColor];
                [progressHUD setMinSize:CGSizeMake(MAX(KWITH - 170, bgViewWidth), 40)];
                progressHUD.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
                progressHUD.backgroundView.color = [UIColor clearColor];
                progressHUD.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
                progressHUD.bezelView.color = [UIColor colorWithWhite:0.0f alpha:0.6038f];
                progressHUD.margin = 10;
//                progressHUD.yOffset = SCREEN_HEIGHT *0.4;
                if (isAutoHiden) {
                    [progressHUD hideAnimated:YES afterDelay:1.5f];
                }
                break;
            default:
                break;
        }
    });
}
+ (void)showNewHUDWithImage:(UIImage *)icon text:(NSString *)text isAutoHiden:(BOOL)isAutoHiden {
    
    if ([SVProgressHUD isVisible]) {
        [SVProgressHUD dismissLong];
    }
    dispatch_async(dispatch_get_main_queue(), ^{
        LNProgressHUD *progressHUD = [LNProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
        progressHUD.label.text = text;
        progressHUD.label.font = [UIFont systemFontOfSize:kTextFontSize];
        progressHUD.contentColor = [UIColor whiteColor];
        progressHUD.label.numberOfLines = 0;
        [progressHUD setMinSize:CGSizeMake(bgViewWidth, bgViewWidth)];
        progressHUD.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
        progressHUD.backgroundView.color = [UIColor colorWithWhite:0.f alpha:0.f];
        progressHUD.bezelView.color = [[UIColor blackColor] colorWithAlphaComponent:0.8];
        progressHUD.margin = 10;
        progressHUD.mode = MBProgressHUDModeCustomView;
        if (icon) {
            LoadingImageView *successIcon = [[LoadingImageView alloc]initWithImage:icon];
            progressHUD.customView = successIcon;
        }
        [progressHUD hideAnimated:YES afterDelay:1.5f];
    });
}
/** 在 window 上添加一个 自定义图标和文字 HUD */
+ (void)showHUDIcon:(UIImage *)image text:(NSString *)text isAutoHiden:(BOOL)isAutoHiden {
    LNProgressHUD *progressHUD = [LNProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
    progressHUD.label.text = text;
    progressHUD.label.font = [UIFont systemFontOfSize:kTextFontSize];
    progressHUD.label.textColor = [UIColor whiteColor];
    progressHUD.label.numberOfLines = 2;
    [progressHUD setMinSize:CGSizeMake(bgViewWidth, bgViewWidth)];
    progressHUD.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
    progressHUD.backgroundView.color = [UIColor colorWithWhite:0.f alpha:0.4f];
    progressHUD.bezelView.color = [UIColor whiteColor];
    progressHUD.mode = MBProgressHUDModeCustomView;
    UIImageView *successIcon = [[UIImageView alloc]initWithImage:image];
    progressHUD.customView = successIcon;
    if (isAutoHiden) {
        [progressHUD hideAnimated:YES afterDelay:1.5f];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
